package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.CareOrderItem;
import com.sixth.core.entity.OrderCharge;
import com.sixth.core.entity.OrderChargeItem;
import com.sixth.core.vo.R;
import com.sixth.mapper.CareOrderItemMapper;
import com.sixth.mapper.OrderChargeItemMapper;
import com.sixth.mapper.OrderChargeMapper;
import com.sixth.service.OrderChargeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 李劲锋
 * @create: 2024-08-16 14:29
 **/
@Service
public class OrderChargeItemServiceImpl extends ServiceImpl<OrderChargeItemMapper, OrderChargeItem> implements
        OrderChargeItemService {
    @Autowired
    protected OrderChargeItemMapper orderChargeItemMapper;

    @Autowired
    private OrderChargeMapper orderChargeMapper;

    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Override
    public R<List<OrderChargeItem>> orderItemList(String orderId) {
        // 创建查询条件，用于查询订单项列表
        QueryWrapper<OrderChargeItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);

        // 查询并返回订单项列表
        List<OrderChargeItem> orderChargeItems = orderChargeItemMapper.selectList(wrapper);
        return R.ok(orderChargeItems);
    }

    @Override
    public R payOrderById(String orderId) {
        // 1. 根据订单号和未支付状态 (status = 0) 查询出订单项信息
        QueryWrapper<OrderChargeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("status", 0);  // 查询未支付的订单项

        List<OrderChargeItem> orderChargeItems = orderChargeItemMapper.selectList(queryWrapper);
        if (!orderChargeItems.isEmpty()) {
            // 2. 更新orderChargeItem项的支付状态为已支付 (status = 1)
            for (OrderChargeItem item : orderChargeItems) {
                item.setStatus("1");
                orderChargeItemMapper.update(item, new QueryWrapper<OrderChargeItem>().eq("item_id", item.getItemId()));
            }

            // 3. 更新orderCharge的状态为已支付 (order_status = 1) 和支付类型 (pay_type = 0)
            OrderCharge order = new OrderCharge();
            order.setOrderStatus("1");
            order.setPayType("0");
            order.setUpdateTime(new Date());

            QueryWrapper<OrderCharge> queryWrapperOrder = new QueryWrapper<>();
            queryWrapperOrder.eq("order_id", orderId);
            queryWrapperOrder.eq("order_status", "0");  // 仅更新未支付的订单

            orderChargeMapper.update(order, queryWrapperOrder);

            // 4. 查询并更新关联的 CareOrderItem 状态
            List<String> itemIds = orderChargeItems.stream()
                    .map(OrderChargeItem::getItemId)
                    .collect(Collectors.toList());

            if (!itemIds.isEmpty()) {
                // 如果有需要更新的 CareOrderItem，更新其状态为已支付 (status = 1)
                QueryWrapper<CareOrderItem> careOrderItemQuery = new QueryWrapper<>();
                careOrderItemQuery.in("item_id", itemIds);

                CareOrderItem careOrderItem = new CareOrderItem();
                careOrderItem.setStatus("1");
                careOrderItemMapper.update(careOrderItem, careOrderItemQuery);
            }
        }

        // 5. 返回成功结果
        return R.ok(200);
    }

}